<?php

use Illuminate\Database\Seeder;

class AdminPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 创建权限
        $permission = new \App\Admin\Models\AdminPermission();
        $permission->name = 'system';
        $permission->description = '系统管理';
        $permission->save();

        // 创建角色
        $role = new \App\Admin\Models\AdminRole();
        $role->name = 'sys-manager';
        $role->description = '系统管理员';
        $role->save();

        // 角色分配权限
        $permission = \App\Admin\Models\AdminPermission::query()->where('name', 'system')->first();
        $role = \App\Admin\Models\AdminRole::query()->where('name', 'sys-manager')->first();
        $role->permissions()->save($permission);

        // 用户分配角色
        $user = \App\Admin\Models\AdminUser::query()->where('name', 'admin')->first();
        $role = \App\Admin\Models\AdminRole::query()->where('name', 'sys-manager')->first();
        $user->roles()->save($role);

        // 创建其他权限
        $this->createPermissions();
        info('make permissions successful.');
    }

    protected function createPermissions()
    {
        // 创建其他权限
        $permissions = [
            [
                'name' => 'post',
                'description' => '文章管理',
            ],
            [
                'name' => 'topic',
                'description' => '专题管理',
            ],
            [
                'name' => 'notice',
                'description' => '通知管理',
            ],
        ];
        foreach ($permissions as $data) {
            $permission = new \App\Admin\Models\AdminPermission();
            $permission->name = $data['name'];
            $permission->description = $data['description'];
            $permission->save();
        }
    }
}
